/**
 * Created by Baggio on 2017-11-23.
 */
$(function () {
    getOrderList();

    $(".background_box").click(function(){
        $(this).hide();
        $(".logistics_box").hide();
    });
    $(".logistics_box .title .esc").click(function(){
        $(".logistics_box").hide();
        $(".background_box").hide();
    })

    $(".refund_reason .button_box button").click(function () {
        var orderId=$(".refund_reason").attr("orderId");
        var refundReason=$(".refund_reason .reason_box").html();
        var postData = {orderId: orderId,refundReason:refundReason}
        $.ajax({
            type: "post",
            url: "/order/refund.html",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                console.log(res);
                if (res.code != 1) {
                    alert(res.msg);
                    return;
                }
                alert("已提交退款申请！");
                $(".refund_reason").hide();
            }

        });
    });
    $(".refund_reason .esc").click(function () {
        $(".refund_reason").hide();
    });
});
function setPic(productEntityId, obj) {
    var postData = {resourceRelationId: productEntityId, resourceRelationType: 4}
    $.ajax({
        type: "post",
        url: "/resource/getResourceList.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            var data = res.data;
            var pic = "";
            if (data.length > 0) {
                pic = picPath + data[0].resourceUrl;
                obj.attr('src', pic);
            }
            else
            {obj.parent().hide();}


        }

    });
}
function getOrderList() {
    var userId = $("#userId").val();
    var postData = {};
    $.ajax({
        type: "post",
        url: "/order/getOrderList.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }

            showData(res.data.list);
        }
    });
}
function showData(orderList) {
    var html = '';
    console.log(orderList.length);
    for (var i = 0; i < orderList.length; i++) {
        html += ' <div class="orders">';
        html += '  <div class="orders-head">';
        html += '   <p>';
        html += '   <b>' + (new Date(orderList[i].createTime)).Format("yyyy-MM-dd") + '</b>';
        html += '    <span>订单号:' + orderList[i].orderId + '</span>';
        html += '  </p>';

        html += '  <a href="javascript:void(0)"><b class="b-box" onclick="cancelOrder(' + orderList[i].orderId + ');">删除订单</b></a>';

        html += '      </div>';

        for (var j = 0; j < orderList[i].orderItemList.length; j++) {
            html += '    <div class="product">';
            html += '    <div class="img-box">';
            html += '    <div class="product-img">';
            html += '     <img src="" alt="" data-id="' + orderList[i].orderItemList[j].productEntity.productEntityId + '"/>';
            html += '    </div>';

            html += '    <div class="product-name">';
            html += '    <p class="name">';
            html += '    <b>' + orderList[i].orderItemList[j].productEntity.productEntityName + '</b>';
            html += '      <span>￥&nbsp;' + orderList[i].orderItemList[j].productEntity.price/100 + '</span>';
            html += '  </p>';

            html += '   <p class="model">';
            html += '   <b> </b>';
            html += '    <span>￥&nbsp;' + orderList[i].orderItemList[j].price/100 + '</span>';
            html += ' </p>';

            html += '  <p class="color">';
            html += '     <span style="background: ' + orderList[i].orderItemList[j].productEntity.productEntityColourCode + ';"></span>';
            html += '    <b>' + orderList[i].orderItemList[j].productEntity.productEntityColour + '</b>';
            html += '    </p>';
            html += '   </div>';
            html += '    </div>';
            html += '    <div class="price">';
            html += '     <p class="money">￥&nbsp;' + orderList[i].orderItemList[j].price/100 + '</p>';
            html += '   <p>(含运费：￥0.00)</p>';
            html += '  </div>';

            html += '   <div class="logistics">';
            html += '     <p class="indelivery"><a href="javascript:void(0)">' + getOrderStatus(orderList[i].status) + '</a></p>';
            html += '    </div>';

            html += '     </div>';
        }

        html += '    <div class="apply">';
        if (orderList[i].status == 2) {
            html += '    <a href="javascript:void(0)" onclick="refund(' + orderList[i].orderId + ')">申请退款</a>';
        }
            if (orderList[i].status == 13) {
                html += '    <a href="javascript:void(0)" onclick="refund(' + orderList[i].orderId + ')">申请退款</a>';
            html += '    <a href="javascript:void(0)" onclick="orderConfirm(' + orderList[i].orderId + ')">确认收货</a>';
            html += '    <a href="javascript:void(0)" onclick="checkLogistics('+orderList[i].orderId+');">查看物流</a>';
        }
        if (orderList[i].status == 5) {


            html += '    <a href="javascript:void(0)" onclick="orderConfirm(' + orderList[i].orderId + ')">确认收货</a>';
            html += '    <a href="javascript:void(0)" onclick="checkLogistics('+orderList[i].orderId+');">查看物流</a>';
        }

        html += '    </div>';

        html += '     <div class="foot-payment">';
        html += '     <p>';
        html += '     合计:<span>￥' + orderList[i].orderPrice / 100 + '</span>';
        if(orderList[i].status == 1){
            html += '  <a href="javascript:void(0)" onclick="toPay(' + orderList[i].orderId + ')">去支付</a>';
        }
        html += '   </p>';
        html += '    </div>';
        html += '    </div>';
    }
    $("#orderList").html(html);
    $(".product img").each(function () {
        var productEntityId = $(this).attr("data-id");
        setPic(productEntityId, $(this));
    })

}
function checkLogistics(orderItemId) {
    var postData = {orderItemId: orderItemId}
    $.ajax({
        type: "post",
        url: "/logistics/checkLogistics.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
          //  alert(res.data);


            var info=JSON.parse(res.data);
            var logisticCode=info.LogisticCode;
            var shipperCode=info.ShipperCode;
            var traces=info.Traces;
            console.log(logisticCode);
            console.log(shipperCode);
            console.log(traces);

            var html="";
            for(index in traces){
                var trace=traces[index];
                console.log(trace);
                html+='<div>' + trace.AcceptStation + '</div>';
                html+='<div class="AcceptTime"><span>'+  trace.AcceptTime + '</span></div>';
                html+='<hr/>';
            }
            $(".logistics_box .logistics_information").html(html);
            $(".logistics_box").show();
            $(".background_box").show();

        }

    });
}
function toPay(orderId) {
    location.href = "/pay/" + orderId + ".html"
}
function getOrderStatus(status) {
    var statusStr = ""
    switch (status) {

        case -2:
            statusStr = "删除";
            break;
        case -1:
            statusStr = "已过期";
            break;
        case 0:
            statusStr = "已取消";
            break;
        case 1:
            statusStr = "未支付";
            break;
        case 2:
            statusStr = "已支付";
            break;
        case 5:
            statusStr = "交易完成";
            break;
        case 6:
            statusStr = "申请退款";
            break;
        case 7:
            statusStr = "取消退款";
            break;
        case 8:
            statusStr = "已退款";
            break;
        case 9:
            statusStr = "交易完成";
            break;
        case 12:
            statusStr = "待发货";
            break;
        case 13:
            statusStr = "已发货";
            break;
        case 14:
            statusStr = "已收货";
            break;
        default:
            statusStr = status;
            break;
    }
    return statusStr;
}
function cancelOrder(orderId) {
    var postData = {orderId: orderId}
    $.ajax({
        type: "post",
        url: "/order/cancelOrder.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            alert("订单取消成功！");
        }

    });
}
function refund(orderId) {
    $(".refund_reason").attr("orderId",orderId);
    $(".refund_reason").show();
}
function cancelRefund(orderItemId) {
    var postData = {orderItemId: orderItemId}
    $.ajax({
        type: "post",
        url: "/order/cancelRefund.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            alert("取消退款成功！");
        }

    });
}
function orderConfirm(orderId) {
    var postData = {orderId: orderId}
    $.ajax({
        type: "post",
        url: "/order/orderConfirm.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            alert("确认收货成功！");
        }

    });
}